package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.io.File;
import java.util.HashMap;

import javax.swing.JRadioButton;

import code.Language;
import code.Settings;
import code.SymmetricEncDec;

public class SettingsMain extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JRadioButton jRadioButton = null;
	private JLabel jLabel3 = null;
	private JRadioButton jRadioButton1 = null;
	private JLabel jLabel4 = null;
	private JButton jButton= null;
	ButtonGroup rbgroup = new ButtonGroup();
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	private String language = null;
	private HashMap labels;
	ATOCOmain atocoframe;
	private Settings s;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;

	/**
	 * This is the default constructor
	 */
	public SettingsMain(ATOCOmain atocoframe) {
		super();
		this.atocoframe=atocoframe;
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "SettingsMain", (String) s.global.get("installPath"));
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel8 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/savefile.gif"));
		jLabel8.setBounds(new Rectangle(331, 254, 105, 58));
		jLabel8.setText("Select");
		jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				browseFileLocation();
			}
		});
		jLabel7 = new JLabel();
		jLabel7.setBounds(new Rectangle(214, 266, 101, 16));
		jLabel7.setText("File Location :");
		jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/filechooser.gif"));
		jLabel6.setBounds(new Rectangle(334, 155, 120, 69));
		jLabel6.setText((String) labels.get("jLabel6"));
		jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				//browseKeyStore();
			}
		});
		jLabel5 = new JLabel();
		jLabel5.setBounds(new Rectangle(212, 173, 120, 16));
		jLabel5.setText((String) labels.get("jLabel5"));
		jLabel4 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/usa.gif"));
		jLabel4.setBounds(new Rectangle(420, 85, 65, 46));
		jLabel3 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/turkey.gif"));
		jLabel3.setBounds(new Rectangle(320, 85, 65, 46));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(216, 102, 70, 16));
		jLabel2.setText((String) labels.get("jLabel2"));
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(215, 29, 310, 39));
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 14));
		
		jLabel = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/settingsicon.gif"));
		jLabel.setBounds(new Rectangle(28, 22, 127, 134));
		this.setSize(751, 453);
		this.setLayout(null);
		this.add(jLabel, null);
		this.add(jLabel1, null);
		this.setBackground(Color.WHITE);
		this.add(jLabel2, null);
		this.add(getJRadioButton(), null);
		this.add(jLabel3, null);
		this.add(getJRadioButton1(), null);
		this.add(jLabel4, null);
		this.add(jLabel5, null);
		this.add(jLabel6, null);
		this.add(getJButton());
		this.add(jLabel7, null);
		this.add(jLabel8, null);
	}

	/**
	 * This method initializes jRadioButton	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setBounds(new Rectangle(300, 100, 20, 20));
			jRadioButton.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton.isSelected())
					{
						language="TUR";
					}

				}
			});
			rbgroup.add(jRadioButton);
		}
		return jRadioButton;
	}

	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(332, 339, 120, 40));
			jButton.setText((String) labels.get("jButton"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println(language);
					if(language!=null){
						s.change("language", language);
						atocoframe.changeLanguage();
						atocoframe.contentPane(new SettingsMain(atocoframe));
						
					}
					
				}
			});
		}
		return jButton;
	}
	/**
	 * This method initializes jRadioButton1	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(400, 100, 20, 20));
			jRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton1.isSelected())
					{
						language="ENG";
					}

				}
			});
			rbgroup.add(jRadioButton1);
		}
		return jRadioButton1;
	}
	
	public void browseFileLocation(){
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle("ATOCO");
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setMultiSelectionEnabled(false);
		fc.showOpenDialog(this); 

		try { 
			// Open an input stream 
			File f= fc.getSelectedFile();
			//File[] f =fc.getSelectedFiles();
			//System.out.println(f);
			if(!f.equals("null")){
				String st=f.getPath();
				System.out.println(st);
				s.change("filelocation", st);
			    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                        "Your New Location has been Set to: " + st);
			}

		}catch(Exception e){

		}

	}
	
	/*public void browseKeyStore(){
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle("ATOCO");
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fc.setMultiSelectionEnabled(false);
		fc.showOpenDialog(this); 

		try { 
			// Open an input stream 
			File f= fc.getSelectedFile();
			//File[] f =fc.getSelectedFiles();
			//System.out.println(f);
			if(!f.equals("null")){
				String st=f.getPath();
				System.out.println(st);
				s.change("personalKeyStorePassword", st);
				s.change("caKeyStoreFile", st);
			    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                        "Your New Location has been Set to: " + st);
			}

		}catch(Exception e){

		}

	}*/


}  //  @jve:decl-index=0:visual-constraint="10,10"
